package _220325;

import java.util.Scanner;

/**
 * @author ShadowLim
 * @create 2022-03-25-11:03
 */
public class 合根植物 {
    static int m, n, k;
    static int[][] arr;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        m = scanner.nextInt();
        n = scanner.nextInt();
        k = scanner.nextInt();
        arr = new int[k][2];
        UnionFind uf = new UnionFind(n * m);
        for (int i = 0; i < k; i++) {
            int a = scanner.nextInt(), b = scanner.nextInt();
            uf.merge(a, b);
        }
        System.out.println(uf.getCnt());
        scanner.close();
    }

    static class UnionFind {
        int root[]; // 祖宗节点
        int rank[]; // 树的高度
        int cnt;    // 节点个数

        public UnionFind(int n) {
            this.cnt = n;
            root = new int[n + 1];
            rank = new int[n + 1];
            for (int i = 1; i < n + 1; i++) {
                root[i] = i;
                rank[i] = 1;
            }
        }

        public int getCnt() {
            return cnt;
        }

        public int find(int p) {
            if (root[p] != p) {
                root[p] = find(root[p]);
            }
            return root[p];
        }

        public void merge(int p, int q) {
            int rp = find(p);
            int rq = find(q);
            if (rp == rq) {
                return;
            }
            if (rank[rp] < rank[rq]) {
                root[rp] = rq;
                rank[rq] += rank[rp];
            } else {
                root[rq] = rp;
                rank[rq] += rank[rp];
            }
            cnt--;
        }
    }
}
